package com.travel.photos.http;

import android.support.annotation.NonNull;

import com.google.gson.GsonBuilder;
import com.travel.photos.bean.FindBean;
import com.travel.photos.bean.FindSearchBean;
import com.travel.photos.bean.FindSearchPurPoseBean;
import com.travel.photos.bean.FindSearchTeavelBean;
import com.travel.photos.bean.OtherUserBean;
import com.travel.photos.bean.PhotoRecommentBean;
import com.travel.photos.bean.RecommendBean;
import com.travel.photos.bean.TravelBean;
import com.travel.photos.config.UrlConfig;

import java.io.IOException;
import java.util.Map;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

public class HttpHelper {
    private static volatile HttpHelper singleton;
    private RetrofitApiService service;
    private HttpHelper() {
        service=getRetrofitApiService();
    }
    public static HttpHelper getInstance() {
        if (singleton == null) {
            synchronized (HttpHelper.class) {
                if (singleton == null) {
                    singleton = new HttpHelper();
                }
            }
        }
        return singleton;
    }
    private RetrofitApiService getRetrofitApiService() {
        return getRetrofit().create(RetrofitApiService.class);
    }
    @NonNull
    private Retrofit getRetrofit() {
        return new Retrofit
                .Builder()
                //配置基础的url
                .baseUrl(UrlConfig.BASE_URL)
                //配置提交或者返回的参数的造型方式为gson
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                //返回值可以使用Obserable
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                //使用https时需要配置
                .client(genericClient())
                .build();
    }
    //加header头部
    public static OkHttpClient genericClient() {
        OkHttpClient httpClient = new OkHttpClient.Builder()
                .addInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        Request request = chain.request()
                                .newBuilder()
                                .addHeader("User-Agent", "YoupuTrip/3.2.4.1")
                                .build();
                        return chain.proceed(request);
                    }
                })
                .build();
        return httpClient;
    }
    /**
     *方法举例说明,这里直接使用了rxjava进行回调
     public Observable<String> getRecommend(String gender) {
     return service.getRecomend(gender);
     }
     */
    public Observable<FindBean> getFindData(Map<String,String> map){
        return service.getFind(map);
    }
    public Observable<RecommendBean> getReData(Map<String,String> map){
        return service.getRe(map);
    }
    public Observable<TravelBean> getTravelData(Map<String,String> map){
        return service.getTravel(map);
    }
    public Observable<FindSearchBean> getFindSearchData(Map<String,String> map){
        return service.getFindSearchTravel(map);
    }
    public Observable<FindSearchPurPoseBean> getFindSearchPurPoseData(Map<String,String> map){
        return service.getFindSearchPurPoseTravel(map);
    }
    public Observable<FindSearchTeavelBean> getFindSearchTeavelData(Map<String,String> map){
        return service.getFindSearchTeavelTravel(map);
    }
    public Observable<PhotoRecommentBean> getPhotoRecommentData(Map<String,String> map){
        return service.getPhotoRecommentTravel(map);
    }
    public Observable<OtherUserBean> getOtherUserData(Map<String,String> map){
        return service.getOtherUserTravel(map);
    }
}
